package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import com.ross.web.domain.Users;
import com.ross.web.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

public class UsersDAO extends HibernateUtil {
	
	
	public List<Users> getAllUsers(){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result =  session.createCriteria(Users.class,"users")
					.setProjection(Projections.projectionList()
							.add(Projections.property("users.userid"),"userid")
							.add(Projections.property("users.username"),"username")
							.add(Projections.property("users.password"),"password")
							.add(Projections.property("users.firstname"),"firstname")
							.add(Projections.property("users.middlename"),"middlename")
							.add(Projections.property("users.lastname"),"lastname")
							.add(Projections.property("users.address"),"address")
							.add(Projections.property("users.telephone"),"telephone")
							.add(Projections.property("users.cellphone"),"cellphone")
							.add(Projections.property("users.email"),"email")
							.add(Projections.property("users.datecreated"),"datecreated")
							.add(Projections.property("users.access"),"access")
							.add(Projections.property("users.loginAttempt"),"loginAttempt")
							.add(Projections.property("users.status"),"status")
							).setResultTransformer(new AliasToBeanResultTransformer(Users.class))
							.list();
			
			
			return result;
			
		}finally{
			session.close();
		}
		
	}
	
	public Users getUsersById(int userid){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result =  session.createCriteria(Users.class,"users")
					.setProjection(Projections.projectionList()
							.add(Projections.property("users.userid"),"userid")
							.add(Projections.property("users.username"),"username")
							.add(Projections.property("users.password"),"password")
							.add(Projections.property("users.firstname"),"firstname")
							.add(Projections.property("users.middlename"),"middlename")
							.add(Projections.property("users.lastname"),"lastname")
							.add(Projections.property("users.address"),"address")
							.add(Projections.property("users.telephone"),"telephone")
							.add(Projections.property("users.cellphone"),"cellphone")
							.add(Projections.property("users.email"),"email")
							.add(Projections.property("users.datecreated"),"datecreated")
							.add(Projections.property("users.access"),"access")
							.add(Projections.property("users.loginAttempt"),"loginAttempt")
							.add(Projections.property("users.status"),"status")
							).add(Restrictions.eq("users.userid", userid))
							.setResultTransformer(new AliasToBeanResultTransformer(Users.class))
							.list();
			
			
			return (Users) result.get(0);
			
		}finally{
			session.close();
		}
		
	}
	
	public List<Users> getUsersByUsername(String searchQuery){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result =  session.createCriteria(Users.class,"users")
					.setProjection(Projections.projectionList()
							.add(Projections.property("users.userid"),"userid")
							.add(Projections.property("users.username"),"username")
							.add(Projections.property("users.password"),"password")
							.add(Projections.property("users.firstname"),"firstname")
							.add(Projections.property("users.middlename"),"middlename")
							.add(Projections.property("users.lastname"),"lastname")
							.add(Projections.property("users.address"),"address")
							.add(Projections.property("users.telephone"),"telephone")
							.add(Projections.property("users.cellphone"),"cellphone")
							.add(Projections.property("users.email"),"email")
							.add(Projections.property("users.datecreated"),"datecreated")
							.add(Projections.property("users.access"),"access")
							.add(Projections.property("users.loginAttempt"),"loginAttempt")
							.add(Projections.property("users.status"),"status")
							).add(Restrictions.like("users.username", "%"+searchQuery+"%"))
							.setResultTransformer(new AliasToBeanResultTransformer(Users.class))
							.list();
			
			
			return result;
			
		}finally{
			session.close();
		}
		
	}
	
	public Users getUsersByUsernameExact(String searchQuery){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result =  session.createCriteria(Users.class,"users")
					.setProjection(Projections.projectionList()
							.add(Projections.property("users.userid"),"userid")
							.add(Projections.property("users.username"),"username")
							.add(Projections.property("users.password"),"password")
							.add(Projections.property("users.firstname"),"firstname")
							.add(Projections.property("users.middlename"),"middlename")
							.add(Projections.property("users.lastname"),"lastname")
							.add(Projections.property("users.address"),"address")
							.add(Projections.property("users.telephone"),"telephone")
							.add(Projections.property("users.cellphone"),"cellphone")
							.add(Projections.property("users.email"),"email")
							.add(Projections.property("users.datecreated"),"datecreated")
							.add(Projections.property("users.access"),"access")
							.add(Projections.property("users.loginAttempt"),"loginAttempt")
							.add(Projections.property("users.status"),"status")
							).add(Restrictions.eq("users.username", searchQuery))
							.setResultTransformer(new AliasToBeanResultTransformer(Users.class))
							.list();
			
			
			return result.isEmpty() ? null : (Users) result.get(0);
			
		}finally{
			session.close();
		}
		
	}
	
	public Users getUserAccessByUsername(String username, String password){
		
		Session session = HibernateUtil.getSession();
		List userInfo = new LinkedList();
		
		try{
			
			userInfo =  session.createCriteria(Users.class,"users")
					.setProjection(Projections.projectionList()
							.add(Projections.property("users.userid"),"userid")
							.add(Projections.property("users.username"),"username")
							.add(Projections.property("users.password"),"password")
							.add(Projections.property("users.firstname"),"firstname")
							.add(Projections.property("users.middlename"),"middlename")
							.add(Projections.property("users.lastname"),"lastname")
							.add(Projections.property("users.address"),"address")
							.add(Projections.property("users.telephone"),"telephone")
							.add(Projections.property("users.cellphone"),"cellphone")
							.add(Projections.property("users.email"),"email")
							.add(Projections.property("users.datecreated"),"datecreated")
							.add(Projections.property("users.access"),"access")
							.add(Projections.property("users.loginAttempt"),"loginAttempt")
							.add(Projections.property("users.status"),"status")
							).add(Restrictions.eq("users.username", username))
			.setResultTransformer(new AliasToBeanResultTransformer(Users.class))
			.list();

			return userInfo.isEmpty() ? null : (Users)userInfo.get(0);
			
		}finally{
			session.close();
		}
		
	}
	
	
	public List validateUsernameByPassword(String username, String password){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
		result =  session.createCriteria(Users.class,"users")
				.setProjection(Projections.projectionList()
						.add(Projections.property("users.userid"),"userid")
						.add(Projections.property("users.username"),"username")
						.add(Projections.property("users.password"),"password")
						.add(Projections.property("users.firstname"),"firstname")
						.add(Projections.property("users.middlename"),"middlename")
						.add(Projections.property("users.lastname"),"lastname")
						.add(Projections.property("users.address"),"address")
						.add(Projections.property("users.telephone"),"telephone")
						.add(Projections.property("users.cellphone"),"cellphone")
						.add(Projections.property("users.email"),"email")
						.add(Projections.property("users.datecreated"),"datecreated")
						.add(Projections.property("users.access"),"access")
						.add(Projections.property("users.loginAttempt"),"loginAttempt")
						.add(Projections.property("users.status"),"status")
						).add(Restrictions.and(Restrictions.eq("users.username", username), 
				Restrictions.eq("users.password", password)))
		.setResultTransformer(new AliasToBeanResultTransformer(Users.class))
		.list();
		
		return result;
			
		}finally{
			session.close();
		}
		
	}
	
	public void insert(Users users) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(users);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	
	public void update(Users users) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(users);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
	
	public void delete(Users users) throws Exception{
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(users);
			tx.commit();
		}catch (Exception e) {
			if (tx != null)
				tx.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

}
